/**
 * @(#) LoginController.java
 * Copyright (c) 2011 EdRP, Dayalbagh Educational Institute.
 * All Rights Reserved.
 *
 * Redistribution and use in source and binary forms, with or
 * without modification, are permitted provided that the following
 * conditions are met:
 *
 * Redistributions of source code must retain the above copyright
 * notice, this  list of conditions and the following disclaimer.
 *
 * Redistribution in binary form must reproducuce the above copyright
 * notice, this list of conditions and the following disclaimer in
 * the documentation and/or other materials provided with the
 * distribution.
 *
 *
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED.  IN NO EVENT SHALL ETRG OR ITS CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL,SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * Contributors: Members of EdRP, Dayalbagh Educational Institute
 */
package in.ac.dei.edrp.cms.controller.login;

import in.ac.dei.edrp.cms.dao.login.LoginService;
import in.ac.dei.edrp.cms.domain.login.Login;

import java.io.StringWriter;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;

/**
 * this is Server side controller class for Course Group
 *
 * @version 1.0 11 FEB 2011
 * @author MOHD AMIR
 */
public class LoginController extends MultiActionController {

	/** creating object of LoginService interface */
	private LoginService loginService;
	
	/** defining setter method for object of LoginService interface */
	public void setLoginService(LoginService loginService) {
		this.loginService = loginService;
	}

	/**
	 * This method is used for getting details of login user 
	 * @param request
	 * @param response
	 * @return Model and View containing login user details details
	 */
	public ModelAndView getLoginDetails(HttpServletRequest request, HttpServletResponse response)
	{
		HttpSession session = request.getSession(true);
		Login login=new Login();
		login.setUserName(request.getParameter("userName"));
		login.setPassword(request.getParameter("password"));
		List<Login> loginDetails=loginService.getLoginDetails(login);
		if(loginDetails.size()>0)
		{
			login.setUserId(loginDetails.get(0).getUserId());
			login.setUniversityId(loginDetails.get(0).getUniversityId());
			login.setUserGroupId(loginDetails.get(0).getUserGroupId());
			int i=loginService.updateLastLogin(login);
			if(i>0)
			{
				session.setAttribute("userName", loginDetails.get(0).getUserName());
				session.setAttribute("userId", loginDetails.get(0).getUserId());
				session.setAttribute("universityId", loginDetails.get(0).getUniversityId());
				session.setAttribute("universityName", loginDetails.get(0).getUniversityName());
				session.setAttribute("startDate", loginDetails.get(0).getStartDate());
				session.setAttribute("endDate", loginDetails.get(0).getEndDate());
			}
		}
		return new ModelAndView("login/loginInfo","loginDetails",loginDetails);
	}
	
	/**
	 * This method is used for logout user 
	 * @param request
	 * @param response
	 */
	public void setLogout(HttpServletRequest request, HttpServletResponse response)
	{
		HttpSession session = request.getSession(true);

		session.invalidate();
	}
	
	/**
	 * this method will create the menu 
	 * @param request
	 * @param response
	 */
	public ModelAndView generateMenu(HttpServletRequest request, HttpServletResponse response)
	{
		HttpSession session = request.getSession(true);
		
		session.setAttribute("userGroupId", request.getParameter("userGroupId"));
		session.setAttribute("userGroupName", request.getParameter("userGroupName"));
		
		Login login=new Login();
		login.setUniversityId(session.getAttribute("universityId").toString());
		login.setUserGroupId(session.getAttribute("userGroupId").toString());
		
		StringWriter menu=loginService.getMenu(login);
		
		if(menu.toString().contains("errorMenu:")){
			return new ModelAndView("login/MenuStructure","menu",menu);
		}
		
		return new ModelAndView("login/MenuStructure","menu",menu);
	}
}
